﻿VM = function () {
    this.State = new MethodState();
    this.Halt = false;
};

VM.prototype.ExecuteCommand = function (commandInfo) {
    var command = new Command(this, commandInfo);
    command.Execute();    
};

VM.prototype.Execute = function (commandText) {
    var commandInfo = new CommandInfo(commandText);
    this.ExecuteCommand(commandInfo);
};

VM.prototype.ExecuteProgram = function (commandArray) {
    for (var i = 0; i < commandArray.length; i++) {
        this.State.MethodBody.push(commandArray[i]);
    }

    while (!this.Halt) {
        var currentCommand = this.State.MethodBody[this.State.IP];
        this.State.IP++;

        this.Execute(currentCommand);

        if (this.State.IP >= this.State.MethodBody.length)
            this.Halt = true;
    }
};